<?
require 'lib.php';

$server = "172.21.147.31";
$connectionInfo = array( "Database"=>"ss2g6", "UID"=>"ss2g6", "PWD"=>"group6", 'ReturnDatesAsStrings'=> true );
$conn = sqlsrv_connect( $server, $connectionInfo );

if ($conn == false){
	 die( print_r( sqlsrv_errors(), true));
}
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

$patientId = $_GET['patientId'];

$sql = "SELECT p.name Pname, p.address Paddress, patientId, p.age Page, d.name Dname FROM Patient p, Doctor d WHERE p.patientId = $patientId AND p.doctorId = d.doctorId";
$sql2 = "SELECT * FROM Prescribes p, Doctor d WHERE p.patientId = $patientId AND p.doctorId = d.doctorId";
$sql3 = "SELECT * FROM RegularPatient rp, Doctor d WHERE rp.patientId = $patientId AND rp.doctorId = d.doctorId";

$result1 = sqlsrv_query($conn, $sql, $params, $options);
$patientInfo = sqlsrv_fetch_array( $result1, SQLSRV_FETCH_ASSOC);
$result2 = sqlsrv_query($conn, $sql2, $params, $options);
$result3 = sqlsrv_query($conn, $sql3, $params, $options);
$regularPatient = sqlsrv_fetch_array( $result3, SQLSRV_FETCH_ASSOC);

?>

<? include 'html_head.php'; ?>
    <? include 'header.php'; ?>
    <div class="container-fluid">
      <div class="row-fluid">
        <div class="span2">
                <? include 'sidebar.php' ?>
        </div><!--/span-->
        <div class="span9">
        <a href="patients.php">&laquo; Back to Patient List</a>
        <h2><? echo $patientInfo['Pname'] ?>'s Information</h2>
        <table class="table table-bordered">
        <tr><td>Patient Id</td><td><? echo $patientInfo['patientId']; ?></td></tr>
            <tr><td>Name</td><td><? echo $patientInfo['Pname']; ?></td></tr>
            <tr><td>Address</td><td><? echo $patientInfo['Paddress']; ?></td></tr>
            <tr><td>Age</td><td><? echo $patientInfo['Page']; ?></td></tr>
            <tr><td>Primary Physician</td><td><? echo $patientInfo['Dname']; ?></td></tr>
        </table>

        <h3>Prescriptions <small>(if any)</small></h3>
        <div class="row">
        <? while($prescription = sqlsrv_fetch_array( $result2, SQLSRV_FETCH_ASSOC)){ ?>
           <div class="span3 well">
            Visited Dr.<? echo $prescription['name']; ?> on <br />
            <? echo $prescription['date']; ?>
           </div>
        <? } ?>
        </div>
        
        <h3>Regularly visits <small>(if any)</small></h3>
        <div class="row">
        <? while($reg = sqlsrv_fetch_array( $result3, SQLSRV_FETCH_ASSOC)){ ?>
           <div class="span3 well">
            Dr. <? echo $reg['name']; ?> on <br />
           </div>
        <? } ?>
        </div>
<? include 'html_tail.php'; ?>
